package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Remark;
import cn.wolfcode.luowowo.article.domain.RemarkContent;
import cn.wolfcode.luowowo.article.mapper.RemarkContentMapper;
import cn.wolfcode.luowowo.article.mapper.RemarkMapper;
import cn.wolfcode.luowowo.article.query.RemarkQuery;
import cn.wolfcode.luowowo.article.service.IRemarkService;
import cn.wolfcode.luowowo.cache.service.IScenicRedisService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RemarkServiceImpl implements IRemarkService {
    @Autowired
    private RemarkMapper remarkMapper;
    @Autowired
    private RemarkContentMapper remarkContentMapper;


    @Override
    public void save(Remark remark) {
        remark.setCreateTime(new Date());//设置设置时间

        //保存评论
        remarkMapper.insert(remark);
        RemarkContent content = remark.getRemarkContent();
        content.setId(remark.getId());
        //保存评论内容
        remarkContentMapper.insert(content);
    }

    @Override
    public PageInfo query(RemarkQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<String> list = new ArrayList<>();
        List<Remark> remarks = remarkMapper.selectForList(qo);
        for (Remark remark : remarks) {
            String[] split = remark.getCoverUrl().split(",");
            for (String s : split) {
                list.add(s);
            }
            remark.setCoverUrls(list);
        }
        return new PageInfo(remarks);
    }

    @Override
    public void favornum(Long remarkId, Long uid) {
        //1、查询评论信息
        Remark remark = remarkMapper.selectByPrimaryKey(remarkId);
        //2、获取点赞用户的id集合
        List<Long> userIds = remark.getFavornumUserIds();
        //3、判断用户id是否在点赞用户id集合中，根据用户来进行判断进行什么操作
        if(userIds.contains(uid)){
            //有，表示此时操作是取消点赞的操作
            userIds.remove(uid);//除去用户
            remark.setFavornum(remark.getFavornum()-1);//点赞数-1
        }else {
            //没有，表示此时操作是点赞的操作
            userIds.add(uid);//添加用户
            remark.setFavornum(remark.getFavornum()+1);
        }
        remarkMapper.insert(remark);

    }
}
